//生产业绩
angular.module('SmartCanteenApp.achievement', ['ngRoute', 'ngTouch'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/achievement', {
            templateUrl: 'productionControl/achievement.html',
            controller: 'achievementCtrl'
        });
        $routeProvider.when('/itemSource', {
            templateUrl: 'productionControl/itemSource.html',
            controller: 'itemSourceCtrl'
        });
        $routeProvider.when('/materieMeasures', {
            templateUrl: 'productionControl/materieMeasures.html',
            controller: 'materieMeasuresCtrl'
        });
    }])

    .controller('achievementCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
    	$scope.hideCanteen()
    	$('#reservation').daterangepicker();
		$(".select2").select2();
		$scope.pagination([])
		//获取基础数据
	    $scope.getBaseData = function(){
	    	//请求所用厨师
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/menus/canteens/'+$scope.canteenId+'/chief','','get',function(data){
				$scope.chefs = data.data
				console.log(data)
			})
			//请求当前食堂下的档口
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/windows','','get',function(data){
				$scope.workshops = data.data
				console.log(data)
			})
	    }
	   
   		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				//获取基础数据
		 		$scope.getBaseData();
   		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
			$scope.getBaseData();
		});
		$scope.searchDate = $scope.getNowFormatDate()+' '+'至'+' '+$scope.getNowFormatDate()
		//发送查询条件
		$scope.searchData = {
			"win":"0",
			"chef":"0"
		}
		$scope.searchAchieve = function(){
			var arr = $scope.searchDate.split(" 至 ")
			$scope.searchData.start_time = arr[0]
			$scope.searchData.end_time = arr[1]
			if( $scope.searchData.start_time > $scope.getNowFormatDate() ){
				$scope.showError('开始时间不能大于当前日期')
				return
			}
			if($scope.searchData.chef == '0'){
				$scope.searchData.chef = ''
			}
			if($scope.searchData.win == '0'){
				$scope.searchData.win = ''
			}
			var path = $scope.resultPath+'/wisdomeatery/v1/products/canteens/'+$scope.canteenId+'/achievements?chef_id='+$scope.searchData.chef+'&window_id='+$scope.searchData.win+
				'&start_time='+$scope.searchData.start_time+'&end_time='+$scope.searchData.end_time
			console.log($scope.searchData)
			$scope.getJson(path,'','get',function(data){
				console.log(data)
				$scope.pagination([])
				$scope.pagination(data.data)
				if(data.code == 0){
					if($scope.searchData.chef == ''){
						$scope.searchData.chef = '0'
					}
					if($scope.searchData.win == ''){
						$scope.searchData.win = '0'
					}
				}else{
					$scope.showError(data.msg)
				}
			})
//			$scope.rows =  [
//		        {
//		            "window_name": "档口1", 
//		            "chef_name": "王小二", 
//		            "item_name": "皮蛋瘦肉粥", 
//		            "unit_name": "碗", 
//		            "sale_count": 3, 
//		            "item_total_count": 46
//		        }
//		    ]
		}
    }])

//菜品溯源
	.controller('itemSourceCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
    	$scope.hideCanteen()
    	$scope.pagination([])
    	$('#datepicker').datepicker({
		      autoclose: true
		});
		$(".select2").select2();
		$(".modal-dialog").draggable();//为模态对话框添加拖拽
		$(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
		//获取基础数据
	    $scope.getBaseP = function(){
	    	//请求所有餐别
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/period_times','','get',function(data){
				$scope.selectPeriods = data.data
				console.log(data)
			})
	    }
	   
   		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				//获取基础数据
		 		$scope.getBaseP();
   		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
			$scope.getBaseP();
		});
		//发送查询条件
		$scope.postItemSourceData = {
			"date":$scope.getNowFormatDate(),
			"period_time":"0",
			"userMessage":""
			
		}
		$scope.byWhoEat = true
		$scope.searchItemSource = function(){
			console.log($scope.postItemSourceData)
			if($scope.postItemSourceData.period_time == '0'){
				$scope.showError('请选择餐别')
				return
			}
			if($scope.postItemSourceData.date == ''){
				$scope.showError('请选择时间')
				return
			}
			if($scope.postItemSourceData.userMessage == ''){
				var type = 0
			}else{
				 type = 1
			}
			var path1 = $scope.resultPath+'/wisdomeatery/v1/products/canteens/' + $scope.canteenId
			var path2 = '/sources?type=' + type
			var path3 = '&selected_date=' + $scope.postItemSourceData.date
			var path4 = '&period_time_id=' + $scope.postItemSourceData.period_time
			var path5 = '&user_name=' + $scope.postItemSourceData.userMessage
			var path = path1 + path2 + path3 + path4 +path5
			console.log(path)
			$scope.getJson(path,'','get',function(data){
				if($scope.postItemSourceData.userMessage != ''){
					$scope.byWhoEat = false
				}
				console.log(data)
				$scope.dataSource = data.data
//				$scope.dataSource = [
//			        {
//			            "item_id": "mock", 
//			            "item_name": "mock", 
//			            "sale_count": 35, 
//			            "workshop_name": "mock", 
//			            "workshop_leader_name": "mock", 
//			            "workshop_leader_cellphone": "mock", 
//			            "chef_name": "mock", 
//			            "chef_cellphone": "mock"
//			        }
//			    ]
				$scope.pagination($scope.dataSource)
			})			
		}
		//按人追溯
		$scope.byUse = function(item_name,item_id,workshop_name){
			console.log(item_name)
			$scope.sourceName = item_name
			$scope.workshopName = workshop_name
//			$scope.responseData =  {
//		        "canteen_name": "mock", 
//		        "datetime": "mock", 
//		        "period_name": "mock", 
//		        "window_name": "mock", 
//		        "details": [
//		            {
//		                "user_name": "mock", 
//		                "cellphone": "mock", 
//		                "department_name": "mock", 
//		                "card_type_name": "mock", 
//		                "count": "mock"
//		            }
//		        ]
//		    }
			var basePath = '/wisdomeatery/v1/products/canteens/'+$scope.canteenId+'/source_persons?item_id='+item_id
		    $scope.getJson($scope.resultPath+basePath,'','get',function(data){
		    	console.log(data)
		    	$scope.responseData = data.data[0]
		    	if(data.data.length == 0){
		    		$scope.showError('暂无数据')
		    	}
		    })
			console.log($scope.canteenId) //根据食堂Id查询
			
			$('#showByUse').modal('toggle')
		}
		//按用料追溯
		$scope.byMaterie = function(item_name,item_id,workshop_name){
			$scope.sourceName = item_name
			$scope.workshopName = workshop_name
//			$scope.responseData =  {
//		        "canteen_name": "mock", 
//		        "datetime": "mock", 
//		        "period_name": "mock", 
//		        "window_name": "mock", 
//		        "details": [
//		            {
//		                "name" : "mock", 
//		                "count": "mock", 
//		                "unit" : "mock"
//		            }
//		        ]
//		    }
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/products/canteens/'+$scope.canteenId+'/source_items?item_id='+item_id,'','get',function(data){
				console.log(data)
				$scope.responseData = data.data
			})
			$('#showByMaterir').modal('toggle')
		}
    }])
//原料测算
	.controller('materieMeasuresCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
		$scope.hideCanteen()
		$scope.pagination([])
		$(".select2").select2();
    	$('#datepicker').datepicker({
		      autoclose: true
		});
		if( $scope.getCanteenId() != ''){
		   	$scope.canteenId =  $scope.getCanteenId() ;
		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
		});
		$(".modal-dialog").draggable();//为模态对话框添加拖拽
		$(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
		//搜索生产计划
		$scope.searchProductPlan = function(){
			console.log($scope.productPlanDate)
			if($scope.productPlanDate == undefined){
				$scope.showError('请选择日期')
				return
			}
			
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/production_plans?start_time='+$scope.productPlanDate+'&end_time='+$scope.productPlanDate+'&status=1','','get',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.productPlans = data.data
					if(data.data.length == 0){
						$scope.showError('暂无数据')
					}				
				}else{
					$scope.showError(data.msg)
				}
			})
//			$scope.productPlans = [
//				{
//					'period_name':'早餐',
//					'period_id':'1',
//					'set_user':'李四',
//					'set_time':'2017-7-2',
//				},{
//					'period_name':'午餐',
//					'period_id':'2',
//					'set_user':'李四',
//					'set_time':'2017-7-2',
//				},{
//					'period_name':'晚餐',
//					'period_id':'3',
//					'set_user':'李四',
//					'set_time':'2017-7-2',
//				}
//			]
		}
		$scope.measures = {
			'period_time_names':['未选择']
		}
		//选中生产计划
		$scope.sureSelectPlan = function(){
			var checkedPlans = $("input[name='checkplans']:checked")
			var length = checkedPlans.length
			var arr = []
			if(length == 0){
				$scope.showError('请选择生产计划')
				return
			}
			for(var i = 0;i<length;i++){
				var period_id = $(checkedPlans[i]).attr('data-Did')
				arr.push(period_id)
			}				
			console.log(arr)//根据ID发送查询条件
			$scope.showProducTime = $scope.productPlanDate
			//请求原料测算数据
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/measures?selected_date='+$scope.productPlanDate+'&period_time_id='+arr,'','get',function(data){
				console.log(data)
				$scope.measures = data.data
				if(data.code == 0){
					$scope.pagination($scope.measures.materials)
					$('#select').modal('toggle')					
				}else{
					$scope.showError(data.msg)
				}
			})
//			$scope.measures = {
//		        "periods": "午餐，早餐", 
//		        "materials": [
//		            {
//		                "material_name": "土豆", 
//		                "unit": "斤", 
//		                "count": "60", 
//		                "windows": [
//		                    {
//		                        "name": "档口1", 
//		                        "count": "10"
//		                    }
//		                ]
//		            },{
//		                "material_name": "柿子", 
//		                "unit": "斤", 
//		                "count": "60", 
//		                "windows": [
//		                    {
//		                        "name": "档口1", 
//		                        "count": "10"
//		                    },{
//		                        "name": "档口2", 
//		                        "count": "50"
//		                    },
//		                ]
//		            }
//		        ]
//		    }
//					$scope.pagination($scope.measures.materials)
//					$('#select').modal('toggle')	
			
		}
	}])